from scapy.all import *
def intnum(bynum:bytes):
    num =  int.from_bytes(bynum,byteorder='big',signed=False)
    return num
def read_pcap(fp:str) -> scapy.plist.PacketList:
    package = rdpcap(fp)
    return package
def get_raw(package) -> bytes:
    if package.haslayer(Raw):
        raw = package.getlayer(Raw).load
        #print(raw)
        return raw
    return None
def bytes2str_dict(bdict):
    """
    将字典中的bytes转换成str
    """
    keys = []
    for key in list(bdict.keys()):
        if isinstance(key,bytes):
            keys.append(bytes.decode(key))
        else:
            keys.append(key)
    values_temp = list(bdict.values())
    values = []
    for v in values_temp:
        if isinstance(v,dict):
            values.append(bytes2str_dict(v))
        else:
            #print(bytes.decode(v))
            try:
                values.append(bytes.decode(v))
            except:
                values.append(v)
    #print(keys,values)
    return dict(zip(keys,values))
def dict2strlist(d:dict) -> list:
    """
    将字典转换成ui用的列表树
    """
    next_lay = []
    for k,v in d.items():
        if isinstance(v,dict):
            next_lay.append(f'{k}')
            next_lay.append(dict2strlist(v))
        else:
            next_lay.append(f'{k}: {v}')
    #print(next_lay)

    return next_lay